package castleContent;

import java.util.Queue;
import java.util.concurrent.ArrayBlockingQueue;

public class Barracks extends Building {

	private static final int CAPACITY_PER_LEVEL_INCEREASE = 50;
	private static final int DEFAULT_ARMY_CAPACITY = 50;
	private static final int MAX_ARMY_CAPACITY = 500;
	private int armyCapacity;
	private int presentArmy;
	private int barbarianHPLevel = 1;
	private int barbarianADLevel = 1;
	private int archerHPLEvel = 1;
	private int archers = 0;
	private int barbarians = 0;
	private int wizzards = 0;
	private int archerADLevel = 1;
	private int wizzardADLevel = 1;
	private int wizzardHPLevel = 1;
	private Queue<Unit> army;
	
	Barracks() {
		setArmyCapacity(DEFAULT_ARMY_CAPACITY);
		setPresentArmy(0);
		army = new ArrayBlockingQueue<Unit>(MAX_ARMY_CAPACITY);
	}
	
	Barracks(int armyCapacity, int archers, int barbarians, int wizzards, int archerAd, int archerHp, int barbarianAd, int barbarianHp, int wizzardAd, int wizzardHp) {
		army = new ArrayBlockingQueue<Unit>(MAX_ARMY_CAPACITY); 
		setArchers(archers, archerAd, archerHp);
		setBarbarians(barbarians, barbarianAd, barbarianHp);
		setWizzards(wizzards, wizzardAd, wizzardHp);
		setPresentArmy(archers + barbarians + wizzards);
		setArmyCapacity(armyCapacity);
	}

	
	public void setPresentArmy(int presentArmy) {
		this.presentArmy = presentArmy;
	}

	public int getPresentArmy() {
		return presentArmy;
	}

	public void upgradeBarbarianAD() {
		barbarianADLevel++;
	}

	public void upgradeBarbarianHP() {
		barbarianHPLevel++;
	}
	
	public void upgradeArcherAD() {
		archerADLevel++;
	}

	public void upgradeArcherHP() {
		archerHPLEvel++;
	}
	
	public void upgradeWizzardAD() {
		wizzardADLevel++;
	}

	public void upgradeWizzardHP() {
		wizzardHPLevel++;
	}

	public int getBarbarianHPLevel() {
		return barbarianHPLevel;
	}

	public int getBarbarianADLevel() {
		return barbarianADLevel;
	}

	public int getArcherHPLevel() {
		return archerHPLEvel;
	}

	public int getArcherADLevel() {
		return archerADLevel;
	}

	public int getWizzardADLevel() {
		return wizzardADLevel;
	}

	public int getWizzardHPLevel() {
		return wizzardHPLevel;
	}

	protected Queue<Unit> getArmy() {
		return army;
	}

	public int getArchers() {
		return archers;
	}

	private void setArchers(int archers, int adLevel, int hpLevel) {
		this.archers = archers;
		addArchers(archers, adLevel, hpLevel);
	}

	public int getBarbarians() {
		return barbarians;
	}

	private void setBarbarians(int barbarians, int adLevel, int hpLevel) {
		this.barbarians = barbarians;
		addBarbarians(barbarians, adLevel, hpLevel);
	}

	public int getWizzards() {
		return wizzards;
	}
	
	public void trainBarbarian(int adLevel, int hpLevel) {
		if(presentArmy < armyCapacity) {
			setPresentArmy(getPresentArmy() + 1);
			army.add(new Barbarian(adLevel, hpLevel));	
			barbarians++;
		}
	}
	public void trainArcher(int adLevel, int hpLevel) {
		if(presentArmy < armyCapacity) {
			army.add(new Archer(adLevel, hpLevel));
			setPresentArmy(getPresentArmy() + 1);
			archers++;
		}
	}
	
	public void trainWizzard(int adLevel, int hpLevel) {
		if(presentArmy < armyCapacity) {
			setPresentArmy(getPresentArmy() + 1);
			army.add(new Wizzard(adLevel, hpLevel));
			wizzards++;
		}
	}

	private void setWizzards(int wizzards, int adLevel, int hpLevel) {
		this.wizzards = wizzards;
		addWizzards(wizzards, adLevel, hpLevel);
	}

	public int getArmyCapacity() {
		return armyCapacity;
	}

	private void addBarbarians(int barbarians, int adLevel, int hpLevel) {
		for(int i = 0; i < barbarians; i++) {
			army.add(new Barbarian(adLevel, hpLevel));
		}
	}
	
	private void addWizzards(int wizzards, int adLevel, int hpLevel) {
		for(int i = 0; i < wizzards; i++) {
			army.add(new Wizzard(adLevel, hpLevel));
		}
	}
	
	private void addArchers(int archers, int adLevel, int hpLevel) {
		for(int i = 0; i < archers; i++) {
			army.add(new Archer(adLevel, hpLevel));
		}
	}
	
	@Override
	public void levelUp() {
		super.levelUp();
		setArmyCapacity(getArmyCapacity() + CAPACITY_PER_LEVEL_INCEREASE);
		
	}

	private void setArmyCapacity(int armyCapacity) {
		this.armyCapacity = armyCapacity;
	}

}
